#!/bin/bash
# sb2-config-debian - configure Debian build settings for SB2
#
# Copyright (c) 2009 Nokia Corporation.
# Copyright (C) 2007 Lauri Leukkunen <lle@rahina.org>
# Licensed under GPL version 2

# NOTE: sb2-upgrade-config may call this before a new config has
# been stamped, so "sb2" must be invoked with "-u" always from
# this script!

my_path=$_
if [ $(basename $my_path) != $(basename $0) ]; then
	my_path=$0
	if [ $(basename $my_path) = $my_path ]; then
		my_path=$(which $my_path)
	fi
fi

mapping_mode_for_dpkg_utils=accel

function log_config_action()
{
	tstamp=`/bin/date '+%Y-%m-%d %H:%M:%S'`
	echo "$tstamp	$*" >>$SBOX_CONFIG_DIR/CONFIG-LOG
}

function usage()
{
	cat <<EOF
sb2-config-debian - Configure Debian build settings for SB2
Usage:
	sb2-config-debian [OPTIONS]

This script uses the "dpkg-architecture" tool to detect
values for DEB_HOST_GNU_TYPE, DEB_BUILD_GNU_SYSTEM, etc.
environment variables and stores them to the config directory.

This is normally invoked automatically by "sb2-init".

Options:
    -t target        set target name
    -h               print this help
    -v               verbose operation
EOF
	exit 2
}

if [ -z "$*" ]; then
	usage
fi

verbose=""

while getopts t:hv option
do
	case $option in
	(t) TARGET=$OPTARG ;;
	(h) usage ;;
	(v) verbose=yes ;;
	(*) usage ;;
	esac
done
shift $(($OPTIND - 1))

if [ -n "$*" ]; then
	# has extra arguments.
	usage
fi

# ---------- Check parameters

if [ -z "$SBOX_DIR" ]; then
	SBOX_DIR=$(readlink -f $(dirname $(readlink -f $my_path))/..)
fi

if [ -z "$TARGET" ]; then
        echo "Error: no target given"
        exit 1
fi

# ---------- end of parameter checks

SBOX_CONFIG_DIR=~/.scratchbox2/$TARGET/sb2.config.d 

# Handle recursive execution gracefully
# (due to sb2-upgrade-config).
[ "$SBOX_CONFIG_DEBIAN_UPGRADE" ] && exit 0
export SBOX_CONFIG_DEBIAN_UPGRADE=yes

if [ -f $SBOX_CONFIG_DIR/gcc.config.sh ]; then
	# configured with a cross-compiler;
	# get ARCH from the primary gcc config file
	. $SBOX_CONFIG_DIR/gcc.config.sh
	ARCH=$SB2_GCC_INIT_ARCH
else
	# No gcc. Use the value which was specified to sb2-init
	. $SBOX_CONFIG_DIR/sb2-init-args
	ARCH=$SB2INIT_ARCH
fi

# set DEBIAN_CPU. For some targets it is not the same as $ARCH
DEBIAN_CPU=$ARCH

HOST_ARCH="$(uname -m)"
if echo "$HOST_ARCH" | grep -q "^i.86*"; then
	HOST_ARCH="i[3456]86"
fi
if [ -n "$verbose" ] ; then
	echo "Host architecture is '$HOST_ARCH'"
fi

case "$ARCH" in
$HOST_ARCH*) ;;

arm*) 
	# SBOX_GCC_TARGET from gcc.config.sh..
	if [ -z "$SBOX_GCC_TARGET" ]; then
		DEBIAN_CPU=$ARCH
	else
		echo $SBOX_GCC_TARGET | grep -q -i eabi
		if [ $? == 0 ]; then
			DEBIAN_CPU=armel
		fi
	fi
	;;
esac

case "$ARCH" in
arm*)
	default_DEB_BUILD_ARCH=$DEBIAN_CPU
	default_DEB_BUILD_ARCH_ABI="gnueabi"
	default_DEB_BUILD_ARCH_CPU=$ARCH
	default_DEB_BUILD_GNU_TYPE=$ARCH-linux-gnueabi
	default_DEB_BUILD_GNU_SYSTEM="linux-gnueabi"

	default_DEB_HOST_GNU_TYPE=$ARCH-linux-gnueabi
	default_DEB_HOST_GNU_SYSTEM="linux-gnueabi"
	default_DEB_HOST_ARCH_CPU=$ARCH
	default_DEB_HOST_ARCH_OS="linux"
	;;
*)
	default_DEB_BUILD_ARCH=$DEBIAN_CPU
	default_DEB_BUILD_ARCH_ABI="gnu"
	default_DEB_BUILD_ARCH_CPU=$DEBIAN_CPU
	default_DEB_BUILD_GNU_TYPE=$ARCH-linux-gnu
	default_DEB_BUILD_GNU_SYSTEM=""

	default_DEB_HOST_GNU_TYPE=$ARCH-linux-gnu
	default_DEB_HOST_GNU_SYSTEM=""
	default_DEB_HOST_ARCH_CPU=$DEBIAN_CPU
	default_DEB_HOST_ARCH_OS="linux"
	;;
esac

if [ ! -f $SBOX_CONFIG_DIR/debian.conf ]; then
	# first time here (maybe upgrading the configuration);
	# debian.conf does not exist. Create a temporary
	# file, otherwise "sb2" will fail to run because
	# this config file is mandatory now.
	#
	: >$SBOX_CONFIG_DIR/debian.conf
fi

# test if dpkg-architecture exists and can be executed.
# (Send stderr to /dev/null so that if dpkg-architecture
# does not exits, this won't print extra crap..
# FIXME: This complete debian configuration thing should
# be disabled if the target system is not a debian based OS,
# but just now there is no reliable way to find that out
# automatically; e.g. if the target FS is being set up, and 
# some files (like dpkg-architecture) do not exist yet..)
DPKG_ARCHITECTURE_FULLPATH=$(sb2 -t $TARGET -m $mapping_mode_for_dpkg_utils which dpkg-architecture 2>/dev/null)
if [ $? != 0 ]; then
	# Can't locate or run dpkg-architecture. Set the values here.
	# FIXME: I'm still not sure if these settings are sane,
	# they ought to be the same as they have been in sb2
	# since the pre-historic days, but still...

	if [ -n "$verbose" ] ; then
		echo "dpkg-architecture doesn't exist: Using built-in values"
	fi

	CONFIG_METHOD="used built-in values, arch=$ARCH and debian_cpu=$DEBIAN_CPU"

	cat >$SBOX_CONFIG_DIR/debian.conf <<EOF
# Debian configuration file generated by sb2-config-debian
# ($CONFIG_METHOD)
DEB_BUILD_ARCH=$default_DEB_BUILD_ARCH
DEB_BUILD_ARCH_ABI=$default_DEB_BUILD_ARCH_ABI
DEB_BUILD_ARCH_CPU=$default_DEB_BUILD_ARCH_CPU
DEB_BUILD_GNU_CPU=$ARCH
DEB_BUILD_GNU_TYPE=$default_DEB_BUILD_GNU_TYPE
DEB_BUILD_GNU_SYSTEM=$default_DEB_BUILD_GNU_SYSTEM

DEB_HOST_ARCH=$DEBIAN_CPU
DEB_HOST_ARCH_OS=$default_DEB_HOST_ARCH_OS
DEB_HOST_ARCH_CPU=$default_DEB_HOST_ARCH_CPU

DEB_HOST_GNU_CPU=$ARCH
DEB_HOST_GNU_TYPE=$default_DEB_HOST_GNU_TYPE
DEB_HOST_GNU_SYSTEM=$default_DEB_HOST_GNU_SYSTEM

export DEB_BUILD_ARCH
export DEB_BUILD_ARCH_CPU
export DEB_BUILD_ARCH_ABI
export DEB_BUILD_GNU_CPU
export DEB_BUILD_GNU_TYPE
export DEB_BUILD_GNU_SYSTEM

export DEB_HOST_ARCH
export DEB_HOST_ARCH_OS
export DEB_HOST_ARCH_CPU

export DEB_HOST_GNU_CPU
export DEB_HOST_GNU_TYPE
export DEB_HOST_GNU_SYSTEM
EOF

else
	if [ -n "$verbose" ] ; then
		echo "Found dpkg-architecture at $DPKG_ARCHITECTURE_FULLPATH"
	fi

	CONFIG_METHOD="values autodetected, based on output of $DPKG_ARCHITECTURE_FULLPATH"

	# This is a bit tricky: sb2 may set the DEB_* variables
	# always when a session is created (that happens if
	# the debian build system settings are already active),
	# so we must build a script which clears the settings:
	sb2 -u -t $TARGET -m $mapping_mode_for_dpkg_utils dpkg-architecture -a$DEBIAN_CPU |
		sed -e 's/=.*//' -e 's/^/unset /' >$SBOX_CONFIG_DIR/debian.tmp0.$$
	echo "dpkg-architecture -a$DEBIAN_CPU" >>$SBOX_CONFIG_DIR/debian.tmp0.$$

	# Now we can run the script.
	cat $SBOX_CONFIG_DIR/debian.tmp0.$$ |
		sb2 -u -t $TARGET -m $mapping_mode_for_dpkg_utils >$SBOX_CONFIG_DIR/debian.tmp1.$$

	# the "-aARCH" flag of dpkg-architecture affect the target
	# settings only (e.g. DEB_HOST_*). If we are cross-compiling
	# for a different target architecture, the build host variables 
	# may refer to our real host now; change them. Because the whole
	# idea of scratchbox2 is to virtualize the environment to look like
	# this host was identical to the target system.
	grep '^DEB_HOST_' $SBOX_CONFIG_DIR/debian.tmp1.$$ |
		sed -e 's/^DEB_HOST_/DEB_BUILD_/' >$SBOX_CONFIG_DIR/debian.tmp2.$$
	grep -v '^DEB_BUILD_' $SBOX_CONFIG_DIR/debian.tmp1.$$ \
		>>$SBOX_CONFIG_DIR/debian.tmp2.$$

	# Make sure that DEB_BUILD_ARCH_ABI exists:
	grep -q '^DEB_BUILD_ARCH_ABI=' $SBOX_CONFIG_DIR/debian.tmp2.$$
	if [ $? != 0 ]; then
		echo "DEB_BUILD_ARCH_ABI=$default_DEB_BUILD_ARCH_ABI" \
			>>$SBOX_CONFIG_DIR/debian.tmp2.$$
	fi

	echo "# Debian configuration file generated by sb2-config-debian" \
		>$SBOX_CONFIG_DIR/debian.tmp3.$$
	echo "# ($CONFIG_METHOD)" >>$SBOX_CONFIG_DIR/debian.tmp3.$$
	cat $SBOX_CONFIG_DIR/debian.tmp2.$$ >>$SBOX_CONFIG_DIR/debian.tmp3.$$
	# create separate "export" statements for all variables:
	grep '^DEB_' $SBOX_CONFIG_DIR/debian.tmp2.$$ |
		sed -e 's/^/export /' -e 's/=.*//' >>$SBOX_CONFIG_DIR/debian.tmp3.$$

	mv $SBOX_CONFIG_DIR/debian.tmp3.$$ $SBOX_CONFIG_DIR/debian.conf

	# remove temp. files
	rm $SBOX_CONFIG_DIR/debian.tmp*.$$
fi

log_config_action "Debian build system configured ($CONFIG_METHOD)"

exit 0
